#include "pwm.h"

#define TIMERA_COUNT_OVERFLOW				1550

/* TIMERA unit and clock definition */
#define TIMERA_UNIT2					(M4_TMRA2)
#define TIMERA_UNIT2_CLOCK              (PWC_FCG2_PERIPH_TIMA2)
#define TIMERA_UNIT2_OVERFLOW_INT       (INT_TMRA2_OVF)

/* TIMERA channel 1 Port/Pin definition */
#define TIMERA_UNIT2_CH1                (TimeraCh1)
#define TIMERA_UNIT2_CH1_PORT           (PortA)
#define TIMERA_UNIT2_CH1_PIN            (Pin00)
#define TIMERA_UNIT2_CH1_FUNC           (Func_Tima0)//这个的枚举是几 就代表数据手册中的第几个func 此处是Func4
/* TIMERA channel 2 Port/Pin definition */
#define TIMERA_UNIT2_CH2                (TimeraCh2)
#define TIMERA_UNIT2_CH2_PORT           (PortA)
#define TIMERA_UNIT2_CH2_PIN            (Pin01)
#define TIMERA_UNIT2_CH2_FUNC           (Func_Tima0)
/* TIMERA channel 3 Port/Pin definition */
#define TIMERA_UNIT2_CH3                (TimeraCh3)
#define TIMERA_UNIT2_CH3_PORT           (PortA)
#define TIMERA_UNIT2_CH3_PIN            (Pin02)
#define TIMERA_UNIT2_CH3_FUNC           (Func_Tima0)
/* TIMERA channel 4 Port/Pin definition */
#define TIMERA_UNIT2_CH4                (TimeraCh4)
#define TIMERA_UNIT2_CH4_PORT           (PortA)
#define TIMERA_UNIT2_CH4_PIN            (Pin03)
#define TIMERA_UNIT2_CH4_FUNC           (Func_Tima0)

/********timera单元1 PB0********/
//#define TIMERA_UNIT1					(M4_TMRA1)
//#define TIMERA_UNIT1_CLOCK              (PWC_FCG2_PERIPH_TIMA1)
//#define TIMERA_UNIT1_OVERFLOW_INT       (INT_TMRA1_OVF)
// 
//#define TIMERA_UNIT1_CH6                (TimeraCh6)
//#define TIMERA_UNIT1_CH6_PORT           (PortB)
//#define TIMERA_UNIT1_CH6_PIN            (Pin00)
//#define TIMERA_UNIT1_CH6_FUNC           (Func_Tima0)

/********timera单元4 PC14 PC15********/
#define TIMERA_UNIT4					(M4_TMRA4)	
#define TIMERA_UNIT4_CLOCK              (PWC_FCG2_PERIPH_TIMA4)	
#define TIMERA_UNIT4_OVERFLOW_INT       (INT_TMRA4_OVF)	
															
#define TIMERA_UNIT4_CH5                (TimeraCh5)		
#define TIMERA_UNIT4_CH5_PORT           (PortC)			
#define TIMERA_UNIT4_CH5_PIN            (Pin14)			
#define TIMERA_UNIT4_CH5_FUNC           (Func_Tima0)			

#define TIMERA_UNIT4_CH6                (TimeraCh6)	
#define TIMERA_UNIT4_CH6_PORT           (PortC)		
#define TIMERA_UNIT4_CH6_PIN            (Pin15)		
#define TIMERA_UNIT4_CH6_FUNC           (Func_Tima0)	

void pwmInit(void)
{
	stc_timera_base_init_t stcTimeraInit;
    stc_timera_compare_init_t stcTimerCompareInit;
    stc_timera_hw_startup_config_t stcTimeraHwConfig;
    stc_port_init_t stcPortInit;

    MEM_ZERO_STRUCT(stcTimeraInit);			
    MEM_ZERO_STRUCT(stcTimerCompareInit);	
    MEM_ZERO_STRUCT(stcTimeraHwConfig);		
    MEM_ZERO_STRUCT(stcPortInit);			
		
    PWC_Fcg2PeriphClockCmd(TIMERA_UNIT2_CLOCK, Enable);  
	PORT_SetFunc(TIMERA_UNIT2_CH1_PORT, TIMERA_UNIT2_CH1_PIN, TIMERA_UNIT2_CH1_FUNC, Disable);
    PORT_SetFunc(TIMERA_UNIT2_CH2_PORT, TIMERA_UNIT2_CH2_PIN, TIMERA_UNIT2_CH2_FUNC, Disable);
    PORT_SetFunc(TIMERA_UNIT2_CH3_PORT, TIMERA_UNIT2_CH3_PIN, TIMERA_UNIT2_CH3_FUNC, Disable);
    PORT_SetFunc(TIMERA_UNIT2_CH4_PORT, TIMERA_UNIT2_CH4_PIN, TIMERA_UNIT2_CH4_FUNC, Disable);  
	
    stcTimeraInit.enClkDiv = TimeraPclkDiv1;//TimeraPclkDiv128;
    stcTimeraInit.enCntMode = TimeraCountModeSawtoothWave;
    stcTimeraInit.enCntDir = TimeraCountDirUp;
    stcTimeraInit.enSyncStartupEn = Disable;
    stcTimeraInit.u16PeriodVal = TIMERA_COUNT_OVERFLOW;
    TIMERA_BaseInit(TIMERA_UNIT2, &stcTimeraInit);  
			
    stcTimerCompareInit.u16CompareVal = 0 ;//* 8u / 10u;
    stcTimerCompareInit.enStartCountOutput = TimeraCountStartOutputLow;
    stcTimerCompareInit.enStopCountOutput = TimeraCountStopOutputLow;
    stcTimerCompareInit.enCompareMatchOutput = TimeraCompareMatchOutputLow;
    stcTimerCompareInit.enPeriodMatchOutput = TimeraPeriodMatchOutputHigh;
    stcTimerCompareInit.enSpecifyOutput = TimeraSpecifyOutputInvalid;
    stcTimerCompareInit.enCacheEn = Disable;
    stcTimerCompareInit.enTriangularCrestTransEn = Disable;
    stcTimerCompareInit.u16CompareCacheVal = stcTimerCompareInit.u16CompareVal;

    TIMERA_CompareInit(TIMERA_UNIT2, TIMERA_UNIT2_CH1, &stcTimerCompareInit);
    TIMERA_CompareCmd(TIMERA_UNIT2, TIMERA_UNIT2_CH1, Enable);

    TIMERA_CompareInit(TIMERA_UNIT2, TIMERA_UNIT2_CH2, &stcTimerCompareInit);
    TIMERA_CompareCmd(TIMERA_UNIT2, TIMERA_UNIT2_CH2, Enable);

    TIMERA_CompareInit(TIMERA_UNIT2, TIMERA_UNIT2_CH3, &stcTimerCompareInit);
    TIMERA_CompareCmd(TIMERA_UNIT2, TIMERA_UNIT2_CH3, Enable);

    TIMERA_CompareInit(TIMERA_UNIT2, TIMERA_UNIT2_CH4, &stcTimerCompareInit);
    TIMERA_CompareCmd(TIMERA_UNIT2, TIMERA_UNIT2_CH4, Enable);  
	
    stcTimeraHwConfig.enSpecifyEventStartupEn = Enable;
    stcTimeraHwConfig.enTrigFallingStartupEn = Disable;
    stcTimeraHwConfig.enTrigRisingStartupEn = Disable;

    TIMERA_HwStartupConfig(TIMERA_UNIT2, &stcTimeraHwConfig);
	TIMERA_Cmd(TIMERA_UNIT2, Enable);//Disable   
}

void pwmSet(uint16_t *pwm)
{
    //动力分成1000份
    float coff = TIMERA_COUNT_OVERFLOW * 0.001f;
    uint16_t m1 = pwm[0] * coff; 
    uint16_t m2 = pwm[1] * coff;
    uint16_t m3 = pwm[2] * coff;
    uint16_t m4 = pwm[3] * coff;

    TIMERA_SetCompareValue(TIMERA_UNIT4, TIMERA_UNIT4_CH5, m1);
    TIMERA_SetCompareValue(TIMERA_UNIT2, TIMERA_UNIT2_CH2, m2);
    TIMERA_SetCompareValue(TIMERA_UNIT2, TIMERA_UNIT2_CH1, m3);
    TIMERA_SetCompareValue(TIMERA_UNIT4, TIMERA_UNIT4_CH6, m4);
}


	
void testfunctionpwmInit(void)
{
	stc_timera_base_init_t stcTimeraInit;
    stc_timera_compare_init_t stcTimerCompareInit;
    stc_timera_hw_startup_config_t stcTimeraHwConfig;
    stc_port_init_t stcPortInit;

    MEM_ZERO_STRUCT(stcTimeraInit);			
    MEM_ZERO_STRUCT(stcTimerCompareInit);	
    MEM_ZERO_STRUCT(stcTimeraHwConfig);		
    MEM_ZERO_STRUCT(stcPortInit);			

    PORT_Init(TIMERA_UNIT4_CH5_PORT, TIMERA_UNIT4_CH5_PIN, &stcPortInit);
    PORT_Init(TIMERA_UNIT4_CH6_PORT, TIMERA_UNIT4_CH6_PIN, &stcPortInit);

	
//    PWC_Fcg2PeriphClockCmd(TIMERA_UNIT1_CLOCK, Enable);//用来测试PB0用来测试PB0用来测试PB0
	PWC_Fcg2PeriphClockCmd(TIMERA_UNIT4_CLOCK, Enable);//用来测试PC14PC15用来测试PC14PC15用来测试PC14PC15
     
    //PORT_SetFunc(TIMERA_UNIT1_CH6_PORT, TIMERA_UNIT1_CH6_PIN, TIMERA_UNIT1_CH6_FUNC, Disable);//用来测试PB0用来测试PB0用来测试PB0 
	
    PORT_SetFunc(TIMERA_UNIT4_CH5_PORT, TIMERA_UNIT4_CH5_PIN, TIMERA_UNIT4_CH5_FUNC, Disable);//用来测试PC14PC15用来测试PC14PC15用来测试PC14PC15
	PORT_SetFunc(TIMERA_UNIT4_CH6_PORT, TIMERA_UNIT4_CH6_PIN, TIMERA_UNIT4_CH6_FUNC, Disable);//用来测试PC14PC15用来测试PC14PC15用来测试PC14PC15
   	
    stcTimeraInit.enClkDiv = TimeraPclkDiv1;//TimeraPclkDiv128;
    stcTimeraInit.enCntMode = TimeraCountModeSawtoothWave;
    stcTimeraInit.enCntDir = TimeraCountDirUp;
    stcTimeraInit.enSyncStartupEn = Disable;
    stcTimeraInit.u16PeriodVal = TIMERA_COUNT_OVERFLOW; 
    //TIMERA_BaseInit(TIMERA_UNIT1, &stcTimeraInit);//用来测试PB0用来测试PB0用来测试PB0
	TIMERA_BaseInit(TIMERA_UNIT4, &stcTimeraInit);//用来测试PC14PC15用来测试PC14PC15用来测试PC14PC15		
			
    stcTimerCompareInit.u16CompareVal = 0 ;//* 8u / 10u;
    stcTimerCompareInit.enStartCountOutput = TimeraCountStartOutputLow;
    stcTimerCompareInit.enStopCountOutput = TimeraCountStopOutputLow;
    stcTimerCompareInit.enCompareMatchOutput = TimeraCompareMatchOutputLow;
    stcTimerCompareInit.enPeriodMatchOutput = TimeraPeriodMatchOutputHigh;
    stcTimerCompareInit.enSpecifyOutput = TimeraSpecifyOutputInvalid;
    stcTimerCompareInit.enCacheEn = Disable;
    stcTimerCompareInit.enTriangularCrestTransEn = Disable;
    stcTimerCompareInit.u16CompareCacheVal = stcTimerCompareInit.u16CompareVal;

    //TIMERA_CompareInit(TIMERA_UNIT1, TIMERA_UNIT1_CH6, &stcTimerCompareInit);	//用来测试PB0用来测试PB0用来测试PB0
    //TIMERA_CompareCmd(TIMERA_UNIT1, TIMERA_UNIT1_CH6, Enable);					//用来测试PB0用来测试PB0用来测试PB0

	TIMERA_CompareInit(TIMERA_UNIT4, TIMERA_UNIT4_CH5, &stcTimerCompareInit);	//用来测试PC14PC15用来测试PC14PC15用来测试PC14PC15
    TIMERA_CompareCmd(TIMERA_UNIT4, TIMERA_UNIT4_CH5, Enable);					//用来测试PC14PC15用来测试PC14PC15用来测试PC14PC15
	TIMERA_CompareInit(TIMERA_UNIT4, TIMERA_UNIT4_CH6, &stcTimerCompareInit);	//用来测试PC14PC15用来测试PC14PC15用来测试PC14PC15
    TIMERA_CompareCmd(TIMERA_UNIT4, TIMERA_UNIT4_CH6, Enable);					//用来测试PC14PC15用来测试PC14PC15用来测试PC14PC15

    stcTimeraHwConfig.enSpecifyEventStartupEn = Enable;
    stcTimeraHwConfig.enTrigFallingStartupEn = Disable;
    stcTimeraHwConfig.enTrigRisingStartupEn = Disable;

    //TIMERA_HwStartupConfig(TIMERA_UNIT1, &stcTimeraHwConfig);		//用来测试PB0用来测试PB0用来测试PB0
	//TIMERA_Cmd(TIMERA_UNIT1, Enable);//Disable						//用来测试PB0用来测试PB0用来测试PB0	
    TIMERA_HwStartupConfig(TIMERA_UNIT4, &stcTimeraHwConfig);		//用来测试PC14PC15用来测试PC14PC15用来测试PC14PC15
	TIMERA_Cmd(TIMERA_UNIT4, Enable);//Disable						//用来测试PC14PC15用来测试PC14PC15用来测试PC14PC15	
}


void testfunction(uint16_t thousand)
{
	float coff = TIMERA_COUNT_OVERFLOW * 0.001f;//=1.5
    uint16_t m1 = thousand * coff; 
	
   // TIMERA_SetCompareValue(TIMERA_UNIT1, TIMERA_UNIT1_CH6, m1);
	
    //TIMERA_SetCompareValue(TIMERA_UNIT4, TIMERA_UNIT4_CH5, m1);
	
    TIMERA_SetCompareValue(TIMERA_UNIT4, TIMERA_UNIT4_CH5, 150);
    TIMERA_SetCompareValue(TIMERA_UNIT4, TIMERA_UNIT4_CH6, 150);
}
